package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBadge;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBadgeDaoImpl extends TempDataObjectDao<UserBadge> {

    public Map<Integer, List<UserBadge>> getUserBadgeMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_badge where userId=?";
        Map<Integer, List<UserBadge>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBadge data = parse(rs);
                List<UserBadge> badgeList = dataMap.getOrDefault(data.getBadgeType(), new ArrayList<>());
                badgeList.add(data);
                dataMap.put(data.getBadgeType(), badgeList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<UserBadge> getUserBadgeList(long userId, int badgeType) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_badge where userId=? and badgeType=?";
        List<UserBadge> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, badgeType);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBadge data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public Map<Long, List<UserBadge>> getAllUserBadgeMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_badge ";
        Map<Long, List<UserBadge>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserBadge data = parse(rs);
                List<UserBadge> badgeList = dataMap.getOrDefault(data.getUserId(), new ArrayList<>());
                badgeList.add(data);
                dataMap.put(data.getUserId(), badgeList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserBadge data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_badge(uid,userId,badgeId,badgeType,disperseGoldShow) values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getUid());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBadgeId());
            call.setInt(i++, data.getBadgeType());
            call.setBoolean(i++, data.isDisperseGoldShow());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserBadge data) {
        PreparedStatement call = null;
        String sql = "update t_u_badge set userId = ?,badgeId=?,badgeType=?,disperseGoldShow=? where uid=? ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getBadgeId());
            call.setInt(i++, data.getBadgeType());
            call.setBoolean(i++, data.isDisperseGoldShow());
            call.setString(i++, data.getUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserBadge parseData(ResultSet rs) throws SQLException {
        UserBadge data = new UserBadge();
        data.setUid(rs.getString("uid"));
        data.setUserId(rs.getLong("userId"));
        data.setBadgeId(rs.getInt("badgeId"));
        data.setBadgeType(rs.getInt("badgeType"));
        data.setDisperseGoldShow(rs.getBoolean("disperseGoldShow"));
        return data;
    }
}
